
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { StorageService } from '../service/storage.service'
import { CommonService } from '../service/common.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

    constructor(private router: Router,private storage:StorageService,private common:CommonService) {

    }

    // 路由守卫
    canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot,) {
        // debugger
        var token = null;
        // console.log(this.storage.get('userInfo'),"userInfo的值")
        // if(this.storage.get('userInfo') == null){
        //     debugger
        //     this.common.presentToast('请重新登录！')
        //     this.router.navigateByUrl('login')
        //     return false
        // }
        token = this.storage.get('userInfo');
        if(!token || !token.token){
            this.common.presentToast('登录超时或已在其他设备登录，请重新登录！')
            this.router.navigateByUrl('login')
            return false;
        }else{
            return true;
        }
        // return this.checkLogin();
    }
    
}
